﻿using System;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 处理2011、2051、2061、2031指令
    /// </summary>
    public static class UplineDataInstructionMonitor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(UplineDataInstructionMonitor));

        /// <summary>
        /// 处理2011指令
        /// </summary>
        /// <param name="content"></param>
        public static void Process2011(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport = CommonInformation.GetLetportByMn(mn);
                    if (letport == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute / 10 * 10, 0);
                    var end = begin.AddMinutes(10);
                    RealDataMain master = null;
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        master = pCtx.RealDataMains.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                            .Where(t => t.设备信息ID == letport.ID)
                            .OrderByDescending(t => t.监测时间)
                            .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);
                    }
                    if (master == null)
                    {
                        master = new RealDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            监测时间 = value,
                            污染物类别ID = 2,
                            RTD = null,
                            表显累计量 = null
                        };
                        pCtx.RealDataMains.Add(master);
                    }
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var rtd = Parser.GetDoubleByKey("w00000-Rtd", content);
                        if (rtd.HasValue)
                        {
                            master.RTD = rtd;
                        }
                    }
                    if (codes.Contains(Consts.FlowmeterAccumulation))
                    {
                        var rtd = Parser.GetDoubleByKey(Consts.FlowmeterAccumulation + "-Rtd", content);
                        if (rtd.HasValue)
                        {
                            master.表显累计量 = rtd;
                        }
                    }
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000") && !code.Contains(Consts.FlowmeterAccumulation))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detail = master.RealDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new RealDataDetail
                                {
                                    污染物编码ID = factor.ID,
                                    RTD = 0
                                };
                                master.RealDataDetails.Add(detail);
                            }
                            var rtdSub = Parser.GetDoubleByKey(code + "-Rtd", content);
                            if (rtdSub.HasValue)// && rtdSub.Value >= 0)//detail.RTD <= 0)
                            {
                                detail.RTD = rtdSub.Value;
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2051(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport = CommonInformation.GetLetportByMn(mn);
                    if (letport == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute / 10 * 10, 0);
                    var end = begin.AddMinutes(10);
                    MinuteDataMain master = pCtx.MinuteDataMains.Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                          .Where(t => t.设备信息ID == letport.ID)
                          .OrderByDescending(t => t.监测时间)
                          .FirstOrDefault(t => t.监测时间 >= begin && t.监测时间 < end);

                    if (master == null)
                    {
                        master = new MinuteDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            监测时间 = datatime.Value,
                            污染物类别ID = 2,
                            时段量 = null
                        };
                        pCtx.MinuteDataMains.Add(master);
                    }
                    //TODO:应该是流量的后来，因子的先来，然后因子的就不重新计算
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou1 = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou1.HasValue)
                        {
                            master.时段量 = cou1;
                        }
                    }
                    var cou = master.时段量;
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detail = master.MinuteDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new MinuteDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.MinuteDataDetails.Add(detail);
                            }
                            var avg = Parser.GetDoubleByKey(code + "-Avg", content);
                            if (avg != null)
                            { detail.Avg = avg; }
                            var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                if (couSub != null)
                                {
                                    detail.Cou = couSub;
                                }
                                else if (avg != null)
                                {
                                    detail.Cou = cou * avg / 1000.0;
                                }
                            }
                            var max = Parser.GetDoubleByKey(code + "-Max", content);
                            if (max != null)
                            { detail.Max = max; }
                            var min = Parser.GetDoubleByKey(code + "-Min", content);
                            if (min != null)
                            { detail.Min = min; }
                        }
                    }



                    pCtx.SaveChanges();
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = master.MinuteDataDetails;
                        foreach (var item in details)
                        {
                            item.Cou = master.时段量 * item.Avg / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2061(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport = CommonInformation.GetLetportByMn(mn);
                    if (letport == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, value.Hour, 0, 0);
                    var end = begin.AddHours(1);
                    var master = pCtx.HourDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new HourDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            监测时间 = value,
                            污染物类别ID = 2
                        };
                        pCtx.HourDataMains.Add(master);
                    }
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou1 = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou1 != null)
                        {
                            master.时段量 = cou1;
                        }
                    }
                    var cou = master.时段量;
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detail = master.HourDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new HourDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.HourDataDetails.Add(detail);
                            }
                            var avg = Parser.GetDoubleByKey(code + "-Avg", content);
                            if (avg != null)
                            { detail.Avg = avg; }
                            var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                if (couSub != null)
                                {
                                    detail.Cou = couSub;
                                }
                                else if (avg != null)
                                {
                                    detail.Cou = cou * avg / 1000.0;
                                }
                            }

                            var max = Parser.GetDoubleByKey(code + "-Max", content);
                            if (max != null)
                            { detail.Max = max; }
                            var min = Parser.GetDoubleByKey(code + "-Min", content);
                            if (min != null)
                            { detail.Min = min; }
                        }
                    }
                    pCtx.SaveChanges();
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = master.HourDataDetails;
                        foreach (var item in details)
                        {
                            item.Cou = master.时段量 * item.Avg / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Process2031(string content)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var datatime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!datatime.HasValue) return;
                    var letport = CommonInformation.GetLetportByMn(mn);
                    if (letport == null) return;
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    var value = datatime.Value;
                    var begin = new DateTime(value.Year, value.Month, value.Day, 0, 0, 0);
                    var end = begin.AddDays(1);
                    var master = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new DayDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            监测时间 = begin,
                            污染物类别ID = 2
                        };
                        pCtx.DayDataMains.Add(master);
                    }
                    master.监测时间 = begin;
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou1 = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou1 != null)
                        {
                            master.时段量 = cou1;
                        }
                    }
                    var cou = master.时段量;
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detail = master.DayDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new DayDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.DayDataDetails.Add(detail);
                            }
                            var avg = Parser.GetDoubleByKey(code + "-Avg", content);
                            if (avg != null)
                            { detail.Avg = avg; }
                            var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                if (couSub != null)
                                {
                                    detail.Cou = couSub;
                                }
                                else if (avg != null)
                                {
                                    detail.Cou = cou * avg / 1000.0;
                                }
                            }
                            var max = Parser.GetDoubleByKey(code + "-Max", content);
                            if (max != null)
                            { detail.Max = max; }
                            var min = Parser.GetDoubleByKey(code + "-Min", content);
                            if (min != null)
                            { detail.Min = min; }
                        }
                    }
                    pCtx.SaveChanges();
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = master.DayDataDetails;
                        foreach (var item in details)
                        {
                            item.Cou = master.时段量 * item.Avg / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void SumDayDataFromHourDataTriggerBy2061(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day, 0, 0, 0);
                    var end = begin.AddDays(1);
                    var hourMains = pCtx.HourDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (hourMains.Count < 1) return;
                    var master = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new DayDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.DayDataMains.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = hourMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = hourMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailHours =
                                pCtx.HourDataDetails.Where(t => ids.Contains(t.主小时数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.DayDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new DayDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.DayDataDetails.Add(detail);
                            }

                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.Cou = detailHours.Sum(t => t.Cou);
                            detail.Avg = detailHours.Average(t => t.Avg);
                            detail.Max = detailHours.Max(t => t.Max);
                            detail.Min = detailHours.Min(t => t.Min);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumMonthDataFromDayDataTriggerBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, 1, 0, 0, 0);
                    var begin2 = begin;
                    var end = begin.AddMonths(1);
                    var startSumTime = SumSettings.GetStartSumTime(letport.企业基本信息ID, letport.ID);
                    //var now = DateTime.Now.AddMinutes(-59);
                    if (startSumTime != null && startSumTime.Value.Year == datatime.Value.Year && startSumTime.Value.Month == datatime.Value.Month)
                    {
                        begin = startSumTime.Value;
                    }
                    var dayMains = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (dayMains.Count < 1) return;
                    var master = pCtx.MonthDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin2)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new MonthDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.MonthDataMains.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = dayMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = dayMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailDays =
                                pCtx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.MonthDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new MonthDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.MonthDataDetails.Add(detail);
                            }
                            detail.Avg = detailDays.Average(t => t.Avg);
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.Cou = detailDays.Sum(t => t.Cou);
                            detail.Max = detailDays.Max(t => t.Max);
                            detail.Min = detailDays.Min(t => t.Min);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumYearDataFromDayDataTriggerBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
                    var begin2 = begin;
                    var end = begin.AddYears(1);
                    var startSumTime = SumSettings.GetStartSumTime(letport.企业基本信息ID, letport.ID);
                    //var now = DateTime.Now;
                    if (startSumTime != null && startSumTime.Value.Year == datatime.Value.Year)
                    {
                        begin = startSumTime.Value;
                    }
                    var dayMains = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (dayMains.Count < 1) return;
                    var master = pCtx.YearDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin2)
                        .FirstOrDefault(t => t.监测时间 < end);

                    if (master == null)
                    {
                        master = new YearDataMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.YearDataMains.Add(master);
                    }
                    master.监测时间 = datatime.Value;
                    master.时段量 = dayMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = dayMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailDays =
                                pCtx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.YearDataDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new YearDataDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.YearDataDetails.Add(detail);
                            }
                            detail.Avg = detailDays.Average(t => t.Avg);
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.Cou = detailDays.Sum(t => t.Cou);
                            detail.Max = detailDays.Max(t => t.Max);
                            detail.Min = detailDays.Min(t => t.Min);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2011(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var masterMonitor = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2,
                            数采市电状态ID = 702,
                            数采市电状态时间 = DateTime.Now,
                            总量市电状态ID = 703,
                            阀门状态 = "---",
                            门禁状态 = "---",
                            总量在线状态ID = 703,
                            数采在线状态ID = 702,
                            数采在离线状态时间 = DateTime.Now


                        };
                        pCtx.RealDataMonitorMains.Add(masterMonitor);
                    }
                    masterMonitor.实时数据时间 = datatime;
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var rtd = Parser.GetDoubleByKey("w00000-Rtd", content);
                        if (rtd != null)
                        {
                            masterMonitor.瞬时量 = rtd;
                            masterMonitor.瞬时量备 = rtd * 3.6;
                        }
                    }
                    if (codes.Contains(Consts.FlowmeterAccumulation))
                    {
                        var rtd = Parser.GetDoubleByKey(Consts.FlowmeterAccumulation + "-Rtd", content);
                        if (rtd != null)
                        {
                            masterMonitor.表显累计量 = rtd;
                        }
                    }

                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000") && !code.Contains(Consts.FlowmeterAccumulation))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailMonitor = masterMonitor.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detailMonitor == null)
                            {
                                detailMonitor = new RealDataMonitorDetail
                                {
                                    污染物编码ID = CommonInformation.GetContamCode(code).ID
                                };
                                masterMonitor.RealDataMonitorDetails.Add(detailMonitor);
                            }
                            var rtdSub = Parser.GetDoubleByKey(code + "-Rtd", content);
                            if (rtdSub != null)
                            {
                                detailMonitor.瞬时量 = rtdSub;
                            }
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2051(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var masterMonitor = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2,
                            数采市电状态ID = 702,
                            数采市电状态时间 = DateTime.Now,
                            总量市电状态ID = 703,
                            阀门状态 = "---",
                            门禁状态 = "---",
                            总量在线状态ID = 703,
                            数采在线状态ID = 702,
                            数采在离线状态时间 = DateTime.Now
                        };
                        pCtx.RealDataMonitorMains.Add(masterMonitor);
                    }

                    masterMonitor.分钟数据时间 = datatime;
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou != null)
                        {
                            masterMonitor.分钟排放量 = cou;
                        }
                    }
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailMonitor = masterMonitor.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detailMonitor == null)
                            {
                                detailMonitor = new RealDataMonitorDetail
                                {
                                    污染物编码ID = CommonInformation.GetContamCode(code).ID
                                };
                                masterMonitor.RealDataMonitorDetails.Add(detailMonitor);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                                if (couSub != null)
                                {
                                    detailMonitor.分钟排放量 = couSub;
                                }
                            }
                        }
                    }
                    pCtx.SaveChanges();
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = masterMonitor.RealDataMonitorDetails;
                        foreach (var item in details)
                        {
                            item.分钟排放量 = masterMonitor.分钟排放量 * item.瞬时量 / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2061(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var masterMonitor = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2,
                            数采市电状态ID = 702,
                            数采市电状态时间 = DateTime.Now,
                            总量市电状态ID = 703,
                            阀门状态 = "---",
                            门禁状态 = "---",
                            总量在线状态ID = 703,
                            数采在线状态ID = 702,
                            数采在离线状态时间 = DateTime.Now
                        };
                        pCtx.RealDataMonitorMains.Add(masterMonitor);
                    }

                    masterMonitor.小时数据时间 = datatime;
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou != null)
                        {
                            masterMonitor.小时排放量 = cou;
                        }
                    }

                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailMonitor = masterMonitor.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detailMonitor == null)
                            {
                                detailMonitor = new RealDataMonitorDetail
                                {
                                    污染物编码ID = CommonInformation.GetContamCode(code).ID
                                };
                                masterMonitor.RealDataMonitorDetails.Add(detailMonitor);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                                if (couSub != null)
                                {
                                    detailMonitor.小时排放量 = couSub;
                                }
                            }
                        }
                    }
                    pCtx.SaveChanges();
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = masterMonitor.RealDataMonitorDetails;
                        foreach (var item in details)
                        {
                            item.小时排放量 = masterMonitor.小时排放量 * item.瞬时量 / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void ProcessRealMonitorBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var masterMonitor = pCtx.RealDataMonitorMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2,
                            数采市电状态ID = 702,
                            数采市电状态时间 = DateTime.Now,
                            总量市电状态ID = 703,
                            阀门状态 = "---",
                            门禁状态 = "---",
                            总量在线状态ID = 703,
                            数采在线状态ID = 702,
                            数采在离线状态时间 = DateTime.Now
                        };
                        pCtx.RealDataMonitorMains.Add(masterMonitor);
                    }

                    masterMonitor.日数据时间 = datatime;
                    var codes = Parser.GetPollutantCodes(content);
                    if (codes.Contains("w00000"))
                    {
                        var cou = Parser.GetDoubleByKey("w00000-Cou", content);
                        if (cou != null)
                        {
                            masterMonitor.日排放量 = cou;
                        }
                    }

                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailMonitor = masterMonitor.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detailMonitor == null)
                            {
                                detailMonitor = new RealDataMonitorDetail
                                {
                                    污染物编码ID = CommonInformation.GetContamCode(code).ID
                                };
                                masterMonitor.RealDataMonitorDetails.Add(detailMonitor);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                            {
                                var couSub = Parser.GetDoubleByKey(code + "-Cou", content);
                                if (couSub != null)
                                {
                                    detailMonitor.日排放量 = couSub;
                                }
                            }
                        }
                    }
                    pCtx.SaveChanges();
                    var enterprise = CommonInformation.GetPollutionSourceById(letport.企业基本信息ID);
                    if (enterprise == null) return;
                    if (enterprise.企业名称.Contains("盛达"))
                    {
                        var details = masterMonitor.RealDataMonitorDetails;
                        foreach (var item in details)
                        {
                            item.日排放量 = masterMonitor.日排放量 * item.瞬时量 / 1000.0;
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumRealMonitorDayDataFromHourDataTriggerBy2061(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, datatime.Value.Day, 0, 0, 0);
                    var end = begin.AddDays(1);
                    var hourMains = pCtx.HourDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (hourMains.Count < 1) return;
                    var master = pCtx.RealDataMonitorMains
                       .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);

                    if (master == null)
                    {
                        master = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.RealDataMonitorMains.Add(master);
                    }
                    master.日数据时间 = datatime;
                    master.日排放量 = hourMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = hourMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailHours =
                                pCtx.HourDataDetails.Where(t => ids.Contains(t.主小时数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new RealDataMonitorDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.RealDataMonitorDetails.Add(detail);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.日排放量 = detailHours.Sum(t => t.Cou);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumRealMonitorMonthDataFromDayDataTriggerBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, datatime.Value.Month, 1, 0, 0, 0);
                    var end = begin.AddMonths(1);
                    var startSumTime = SumSettings.GetStartSumTime(letport.企业基本信息ID, letport.ID);
                    //var now = DateTime.Now;
                    if (startSumTime != null && startSumTime.Value.Year == datatime.Value.Year && startSumTime.Value.Month == datatime.Value.Month)
                    {
                        begin = startSumTime.Value;
                    }
                    var dayMains = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (dayMains.Count < 1) return;
                    var master = pCtx.RealDataMonitorMains
                       .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);

                    if (master == null)
                    {
                        master = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.RealDataMonitorMains.Add(master);
                    }
                    master.月数据时间 = datatime;
                    master.月排放量 = dayMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = dayMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailDays =
                                pCtx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new RealDataMonitorDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.RealDataMonitorDetails.Add(detail);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.月排放量 = detailDays.Sum(t => t.Cou);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumRealMonitorYearDataFromDayDataTriggerBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
                   
                    var end = begin.AddYears(1);
                    var startSumTime = SumSettings.GetStartSumTime(letport.企业基本信息ID, letport.ID);
                    //var now = DateTime.Now;
                    if (startSumTime != null && startSumTime.Value.Year == datatime.Value.Year)
                    {
                        begin = startSumTime.Value;
                    }
                    var dayMains = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        .Where(t => t.监测时间 >= begin)
                        .Where(t => t.监测时间 < end)
                        .ToList();
                    if (dayMains.Count < 1) return;
                    var master = pCtx.RealDataMonitorMains
                       .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);

                    if (master == null)
                    {
                        master = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.RealDataMonitorMains.Add(master);
                    }
                    master.年数据时间 = datatime;
                    master.年排放量 = dayMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = dayMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailDays =
                                pCtx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new RealDataMonitorDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.RealDataMonitorDetails.Add(detail);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.年排放量 = detailDays.Sum(t => t.Cou);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        public static void SumRealMonitorTotalDataFromDayDataTriggerBy2031(string content)
        {
            using (var pCtx = new PlatformContext())
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var datatime = Parser.GetDateTimeByKey("DataTime", content);
                if (!datatime.HasValue) return;
                var letport = CommonInformation.GetLetportByMn(mn);
                if (letport == null) return;
                try
                {
                    //var begin = new DateTime(datatime.Value.Year, 1, 1, 0, 0, 0);
                    //var end = begin.AddYears(1);
                    var dayMains = pCtx.DayDataMains
                        .Where(t => t.企业基本信息ID == letport.企业基本信息ID)
                        .Where(t => t.设备信息ID == letport.ID)
                        //.Where(t => t.监测时间 >= begin)
                        //.Where(t => t.监测时间 < end)
                        .ToList();
                    if (dayMains.Count < 1) return;
                    var master = pCtx.RealDataMonitorMains
                       .Where(t => t.企业基本信息ID == letport.企业基本信息ID).FirstOrDefault(t => t.设备信息ID == letport.ID);

                    if (master == null)
                    {
                        master = new RealDataMonitorMain
                        {
                            企业基本信息ID = letport.企业基本信息ID,
                            设备信息ID = letport.ID,
                            污染物类别ID = 2
                        };
                        pCtx.RealDataMonitorMains.Add(master);
                    }
                    master.累计量 = dayMains.Sum(t => t.时段量);

                    var codes = Parser.GetPollutantCodes(content);
                    var ids = dayMains.Select(t => t.ID);
                    foreach (var code in codes)
                    {
                        if (!code.Contains("w00000"))
                        {
                            var factor = CommonInformation.GetContamCode(code);
                            if (factor == null) continue;
                            var detailDays =
                                pCtx.DayDataDetails.Where(t => ids.Contains(t.主日数据ID))
                                    .Where(t => t.污染物编码ID == factor.ID);
                            var detail = master.RealDataMonitorDetails.FirstOrDefault(t => t.污染物编码ID == factor.ID);
                            if (detail == null)
                            {
                                detail = new RealDataMonitorDetail
                                {
                                    污染物编码ID = factor.ID
                                };
                                master.RealDataMonitorDetails.Add(detail);
                            }
                            if (factor.显示排放量 != null && factor.显示排放量.Value)
                                detail.年排放量 = detailDays.Sum(t => t.Cou);
                        }
                    }
                    pCtx.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
    }
}
